package com.haibei.activity.common;
 
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
 
public class SelectPic2Activity extends Activity implements OnClickListener{
        Button cutBtn;
        ImageView imageView;
         
         
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
//                setContentView(R.layout.activity_main);
//                cutBtn=(Button)findViewById(R.id.cutBtn);
//                imageView=(ImageView)findViewById(R.id.myImage);
                cutBtn.setOnClickListener(this);
        }
 
 
        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                case 1:
                        AlertDialog dialog;
                        String[] items = { "本地相册", "相机" };
                        ListAdapter itemlist = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, items);
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setAdapter(itemlist, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int item) {
                                        if (item == 0) {
                                                Intent intent = new Intent();
                                                intent.setType("image/*");
                                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                                startActivityForResult(intent, 100);
                                        } else {
                                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                                startActivityForResult(intent, 101);
                                        }
                                }
                        });
                        dialog = builder.create();
                        dialog.show();
                        break;
 
                default:
                        break;
                }
        }
         
         
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        	
                switch (requestCode) {
                case 100:
                        startPhotoZoom(data.getData()); 
                        break;
                case 101:
                        startPhotoZoom(data.getData()); 
                        break;
                case 102:
                        if(data != null){ 
                                Uri uri= Uri.parse(data.getAction());
                                Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
                                cursor.moveToFirst();
                                String path = cursor.getString(1);
                                Bitmap bitmap=new BitmapFactory().decodeFile(path);
                                imageView.setImageBitmap(bitmap);
                        }
                        break;
                default:
                        break;
                }
        }
         
         
        /** 
      * 裁剪图片方法实现 
          * @param uri 
          */
        public void startPhotoZoom(Uri uri) { 
             Intent intent = new Intent("com.android.camera.action.CROP"); 
             intent.setDataAndType(uri, "image/*"); 
             //下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 
             intent.putExtra("crop", "true"); 
         // aspectX aspectY 是宽高的比例 
             intent.putExtra("aspectX", 1); 
             intent.putExtra("aspectY", 1); 
             // outputX outputY 是裁剪图片宽高 
             intent.putExtra("outputX", 150); 
             intent.putExtra("outputY", 150); 
             startActivityForResult(intent, 102); 
    } 
         
         
 
}